<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台配置控制器
 * @author sunbirder
 */
class ConfigController extends CommonController {

    /**
     * [_search 搜索/获取查询条件]
     * @return [type] [description]
     */
    protected function _search()
    {
        $map = array();
        if(is_numeric($_REQUEST['groupId'])){
            $map['group'] = $_REQUEST['groupId'];
        }
        if($_REQUEST['name']){
            $map['name'] = array('like', '%' . trim($_REQUEST['name']) . '%');
        }
        return $map;
    }

    /**
     * 快熟设置配置数据
     */
    public function setDefaultConfig(){
        $name = I('get.name');
        $data = array();
        $data['value'] = I('get.value');
        $data['updatetime'] = date("Y-m-d H:i:s", time());
        $result = M('config')->where(array('name' => $name))->save($data);
        if (is_numeric($result)) {
            S('DB_CONFIG_DATA', null);
            $this->mtReturn(200, '更新默认信息成功', $_REQUEST['navTabId'], false);
        }
        $this->mtReturn(300, '更新默认信息失败', $_REQUEST['navTabId'], false);
    }

    /**
     * 批量保存配置
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function save($config){
        if($config && is_array($config)){
            $Config = M('Config');
            foreach ($config as $name => $value) {
                $map = array('name' => $name);
                $Config->where($map)->setField('value', $value);
            }
        }
        S('DB_CONFIG_DATA',null);
        $this->success('保存成功！');
    }


    // 获取某个标签的配置参数
    public function group() {
        $groupList  =   C('CONFIG_GROUP_LIST');
        $websiteConfigList = array();
        foreach($groupList as $groupKey=>$groupName){
            if($groupKey==0){
                continue;
            }
            $websiteConfigList[$groupKey]['name'] = $groupName;
            $websiteConfigList[$groupKey]['list'] = M("Config")->where(array('status'=>1, 'group'=>$groupKey))->field('id,name,title,extra,value,remark,type')->order('sort')->select();
        }

        $this->assign('websiteConfigList',$websiteConfigList);
        FPC($websiteConfigList);
        $this->display();
    }

    /**
     * 配置排序
     * @author huajie <banhuajie@163.com>
     */
    public function sort(){
        if(IS_GET){
            $ids = I('get.ids');

            //获取排序的数据
            $map = array('status'=>array('gt',-1));
            if(!empty($ids)){
                $map['id'] = array('in',$ids);
            }elseif(I('group')){
                $map['group']	=	I('group');
            }
            $list = M('Config')->where($map)->field('id,title')->order('sort asc,id asc')->select();

            $this->assign('list', $list);
            $this->meta_title = '配置排序';
            $this->display();
        }elseif (IS_POST){
            $ids = I('post.ids');
            $ids = explode(',', $ids);
            foreach ($ids as $key=>$value){
                $res = M('Config')->where(array('id'=>$value))->setField('sort', $key+1);
            }
            if($res !== false){
                $this->success('排序成功！',Cookie('__forward__'));
            }else{
                $this->error('排序失败！');
            }
        }else{
            $this->error('非法请求！');
        }
    }

}